﻿// JScript 文件

Ext.onReady(function () {
    // 使用表单提示
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var sm = new Ext.grid.CheckboxSelectionModel();

    var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm,
		{ header: '商品编号', width: 80, align: 'center', dataIndex: 'proNO',
		    renderer:function (data, metadata, record, rowIndex, columnIndex, store) {  
		            var proID = store.getAt(rowIndex).get('proID');
		            return '<a href="../product.aspx?proID=' + proID  + '" target="_blank" >' + data + '</a> '; 
                }   },
		{ header: '商品名称', width: 80, align: 'center', dataIndex: 'proName',
		        renderer:function (data, metadata, record, rowIndex, columnIndex, store) {  
		                    var proID = store.getAt(rowIndex).get('proID');
		                    return '<a href="../product.aspx?proID=' + proID  + '" target="_blank" >' + data + '</a> '; 
                        }   },
		{ header: '时间', width: 120, align: 'center', dataIndex: 'addTime' },
        { header: '客户名', width: 100, align: 'center', dataIndex: 'cusLoginName',
            renderer:function(v){if(v=="") return "--";else return v;} },
        { header: '咨询内容', width: 500, align: 'center', dataIndex: 'context' },
        { header: '回复', width: 60, align: 'center', dataIndex: 'replyCount',
            renderer:function(v){if(v==0) return "未回复"; if(v==1) return "已回复";} }  
          
	]);

    cm.defaultSortable = true;

    var ds = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: 'ashx/Consult_GetByPage.ashx'
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        },
		[{ name: 'id', mapping: 'conID', type: 'string' }, 
		 { name: 'proID', type: 'int' },
         { name: 'proNO', type: 'string' },
		 { name: 'proName', type: 'string' },
		 { name: 'addTime', type: 'string' },
         { name: 'cusLoginName', type: 'string' },
         { name: 'context', type: 'string' },
         { name: 'title', type: 'string' } ,
         { name: 'replyCount', type: 'int' }  ,
         { name: 'reply', type: 'string' } 
		])
    });
    var typeData = new Ext.data.SimpleStore( { fields : ["typeValue", "text"], data:[["-1","所有"],["0","未回复"],["1","已回复"]] } );
    var searchbar = new Ext.Toolbar({
        id: 'searchbar',
        items: [ 
		    ' 状态  ： ',
		    new Ext.form.ComboBox({
		        id: 'type',
		        width: 100,
		        store : typeData,
		        mode : "local",
		        editable : false,
		        valueField : "typeValue",
                displayField : "text",
                hiddenName : "status", 
                triggerAction: 'all' 		        
		    }), ' ',
		    
			new Ext.Toolbar.Button({
			    id: 'searchbutton',
			    iconCls: 'icon_search',
			    text: '查询',
			    handler: doSearch
			})
		]
    });

    function doSearch() {
        ds.load(
	        { params: {
	            start: 0,
	            limit: 20, 
	            status:Ext.get('status').getValue()
	        }
	        });
    }
 
    var paging = new Ext.PagingToolbar({
        pageSize: 20,
        store: ds,
        displayInfo: true,
        displayMsg: '显示第{0}条到{1}条记录，一共{2}条',
        items:
		[
 

		 "-", " ", { text: '删除',
		     tooltip: '删除客户评价',
		     iconCls: 'icon-del',
		     handler: function () {

		         var _record = grid.getSelectionModel().getSelected();
		         if (sm.getCount() == 1) {
		             del_record(_record );
		         } else
		             Ext.Msg.alert('提示', '请选择一条记录！');
		         

		     }
		 },
		  "-", " ",
	    { text: '回复',
	        tooltip: '回复',
	        iconCls: 'icon-edit',
	        handler: function () {
	            var _record = grid.getSelectionModel().getSelected();
	            if (sm.getCount() == 1) {
	                 //window.open("salesOrderInfo.aspx?orderID=" + _record.data.id);
	                Reply(_record);                 
	            } else
	                Ext.Msg.alert('提示', '请选择一条记录！');
	        }
	    } 	     
		],
        emptyMsg: "查询记录为空!"
    });
    var grid = new Ext.grid.EditorGridPanel({
        renderTo: 'grid-div',
        cm: cm,
        ds: ds,
        sm: sm, 
        tbar: searchbar,
        bbar: paging,
        height: 500,
        border: true,
        bodyStyle: 'width:1020px',
        stripeRows: true,
        title: '系统日志'
    });

    ds.load({ params: { start: 0, limit: 20} });


    function Reply(record)
    {
        var window_reply = new Ext.Window({
            title: '咨询回复',
            width: 540,
            height: 580,
            resizable: false,
            autoHeight: true,
            modal: true,
            closeAction: 'hide',
            listeners: {
                'hide': function () {
                    this.setTitle('回复咨询');
                }
            },
            items: [{
                xtype: "form",
                labelWidth: 70,
                labelAlign: 'right',
                url: 'ashx/Consult_reply.ashx',
                border: false,
                baseCls: 'x-plain',
                bodyStyle: 'padding:5px 5px 0',
                anchor: '100%',
                defaults: {
                    width: 507,
                    msgTarget: 'side'
                },
                items: [{
                    xtype: "fieldset",
                    title: '',
                    autoHeight: true,
                    items: [{
                        xtype: 'hidden',
                        id: 'consult.conid',
                        name: 'consult.conid',
                        value: record.data.id
                    } ,{
                        width: 400,
                        xtype: 'label',
                        html : record.data.proNO,
                        fieldLabel: "商品编码" 
                    } ,{
                        width: 400,
                        xtype: 'label',
                        html : record.data.contact,
                        fieldLabel: "联系方式" 
                    } ,{
                        width: 400,
                        xtype: 'label',
                        html : record.data.context,
                        fieldLabel: "咨询内容" 
                    } , {
                        width: 390,
                        xtype: "textarea",
                        fieldLabel: "回复",
                        value: record.data.reply,
                        name: "consult.reply", 
                        regexText: "请输入回复内容!",
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        maxLenth:200 
                    } ]
                }],
                buttonAlign: 'center',
                minButtonWidth: 60,
                buttons: [{
                    text: '保存',
                    handler: function (btn) {
                        var frm = this.ownerCt.ownerCt.form;
                        if (frm.isValid()) {
                            btn.disable();
                            var cnfield = frm.findField('consult.reply');
                            frm.submit({
                                waitTitle: '请稍候',
                                waitMsg: '正在提交表单数据,请稍候...',
                                success: function (form, action) {

                                    Ext.Msg.show({
                                        title: '成功提示',
                                        msg: '修改成功!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            //cnfield.focus(true);
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.INFO
                                    });
                                    cnfield.reset();
                                    window_reply.hide();
                                    ds.load();
                                },
                                failure: function () {
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        msg: '添加失败，请联系管理员!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.ERROR
                                    });
                                }
                            });
                        }
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        window_reply.hide();
                    }
                }]
            }]
        });
        window_reply.show();   
            
    }
    
    function del_record(record) {

        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        Ext.Ajax.request({
              url: 'ashx/Consult_Del.ashx' ,
            params:{
                 conId:record.data.id  
            },
            success: function (response) {
                ds.load();
                myMask.hide();
            },
            failure: function () { 
                Ext.Msg.alert('提示', '操作失败！');
                myMask.hide();
            }
        });
    }

 


});